package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.risk.dto.ExamVo;
import com.system.manager.modules.risk.entity.TbTask;
import com.system.manager.modules.risk.service.IStaffStationService;
import com.system.manager.modules.risk.vo.ExamDate;
import com.system.manager.modules.risk.vo.UserDate;
import com.system.manager.modules.risk.entity.StaffStation;
import com.system.manager.modules.risk.entity.TbExam;
import com.system.manager.modules.risk.mapper.TbExamMapper;
import com.system.manager.modules.risk.mapper.TbTaskMapper;
import com.system.manager.modules.risk.service.ITbExamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-11
 */
@Service
public class TbExamServiceImpl extends ServiceImpl<TbExamMapper, TbExam> implements ITbExamService {
    @Autowired
    private TbExamMapper tbExamMapper;

    @Autowired
    private TbTaskMapper tbTaskMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private IStaffStationService iStaffStationService;


    //推送试题
    @Override
    public UserDate<TbExam> findExam(Integer currPage, Integer size, Subject subject) {
        User byId = userService.getById(subject.getId());
        //部门人员
        StaffStation staffStaion = iStaffStationService.findStaffStaionById(byId.getUserId());
        //部门id
        Integer sId = staffStaion.getSId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type_id",sId);
        IPage<TbExam> tbExamIPage = new Page<>(currPage,size);
        page(tbExamIPage,wrapper);
        Math.random();
        UserDate<TbExam> userDate = new UserDate();
        userDate.setCode(0);
        userDate.setMsg("试题");
        userDate.setCount((int)tbExamIPage.getTotal());
        userDate.setData(tbExamIPage.getRecords());
        return userDate;
    }

    //提交试题
    @Override
    public void postExam(List<ExamVo> examVo,Subject subject) {
        int result = 0;
        //计算分值
        for (ExamVo vo : examVo) {
            QueryWrapper wrapper = new QueryWrapper();
            String examMessage = vo.getExamMessage();
            wrapper.eq("exam_Message",examMessage);
            TbExam tbExam = tbExamMapper.selectOne(wrapper);
            String ownAnswer = vo.getOwnAnswer();
            String answer = tbExam.getAnswer();
            String score = tbExam.getScore();
            if(ownAnswer.equals(answer)){
                result += Integer.valueOf(score);
            }
        }
        //上传到教育任务表
        User byId = userService.getById(subject.getId());
        Integer uId = byId.getUserId();
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("u_id",uId);
        TbTask tbTask = tbTaskMapper.selectOne(wrapper1);
        if(tbTask == null){
            TbTask tbTask1 = new TbTask();
            tbTask1.setUId(uId);
            tbTask1.setExam(String.valueOf(result));
            tbTaskMapper.insert(tbTask1);
        }else {
            tbTask.setUId(uId);
            tbTask.setExam(String.valueOf(result));
            tbTaskMapper.updateById(tbTask);
        }
    }
    //查看考试分数
    @Override
    public UserDate findScore(Subject subject) {
        User byId = userService.getById(subject.getId());
        StaffStation staffStaion = iStaffStationService.findStaffStaionById(byId.getUserId());
        Integer uId = staffStaion.getUId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",uId);
        List list = tbTaskMapper.selectList(wrapper);
        TbTask tbTask = (TbTask) list.get(0);
        String exam = tbTask.getExam();
        UserDate userDate = new UserDate();
        userDate.setCode(0);
        userDate.setMsg("考试分数");
        userDate.setCount(1);
        List list1 = new ArrayList();
        list1.add(exam);
        userDate.setData(list1);
        return userDate;
    }

    //返回结果页面
    @Override
    public UserDate<ExamDate> correct(List<ExamVo> examVo,Subject subject) {
        UserDate<ExamDate> userDate = new UserDate();
        userDate.setCode(0);
        userDate.setCount(examVo.size());
        userDate.setMsg("结果页面");
        //做题人
        User byId = userService.getById(subject.getId());
        Integer uId = byId.getUserId();

        List<ExamDate> examDateList = new ArrayList<>();
        for (ExamVo vo : examVo) {
            QueryWrapper wrapper = new QueryWrapper();
            //先找到哪个题
            Integer examId = vo.getExamId();
            wrapper.eq("exam_id",examId);
            TbExam tbExam = tbExamMapper.selectOne(wrapper);
            //试题类型
            String examName = vo.getExamName();
            //试题信息
            String examMessage = vo.getExamMessage();

            //试题分数
            String score = tbExam.getScore();
            //试题解析
            String analytic = tbExam.getAnalytic();
            //判断结果
            String ownAnswer = vo.getOwnAnswer();
            String answer = tbExam.getAnswer();

            //结果表
            ExamDate examDate = new ExamDate();
            if(ownAnswer.equals(answer)){
                examDate.setFlag(true);
                examDate.setScore(score);
            }else {
                examDate.setFlag(false);
                examDate.setScore(String.valueOf(0));
            }
            examDate.setExamName(examName);
            examDate.setExamMessage(examMessage);
            examDate.setExamId(examId);
            examDate.setUId(uId);
            examDate.setOwnAnswer(ownAnswer);
            examDate.setAnswer(answer);
            examDate.setAnalytic(analytic);
            examDateList.add(examDate);
            Integer typeId = tbExam.getTypeId();
            examDate.setSId(typeId);
        }
        userDate.setData(examDateList);
        return userDate;
    }
    //返回评语
    @Override
    public UserDate returnScore(Subject subject) {

        return null;
    }
}
